package com.ruoyi.wechat.utils;

import org.springframework.util.ObjectUtils;

import java.time.Duration;
import java.time.LocalDateTime;

/**
 * <p>
 * 时间工具
 * </p>
 *
 * @author YinHeng
 * @since 2024/3/21
 */
public class TimeUtils {
    /**
     * 计算传入时间到当前时间的时间差
     *
     * @param startTime
     * @return
     */
    public static String costTime(LocalDateTime startTime) {
        return costTime(startTime, LocalDateTime.now());
    }

    /**
     * 计算传入时间到当前时间的时间差
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static String costTime(LocalDateTime startTime, LocalDateTime endTime) {
        String result = "";
        if (ObjectUtils.isEmpty(startTime) || ObjectUtils.isEmpty(endTime)) {
            return result;
        }
        Duration duration = Duration.between(startTime, endTime);

        long hours = duration.toHours();
        long minutes = duration.toMinutes() % 60;

        if (hours < 1) {
            result = minutes + "分";
        } else {
            result = hours + "小时" + minutes + "分";
        }
        return result;
    }
}
